package com.daxt.utils;

import org.apache.commons.lang3.ObjectUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @description:
 * @author: lig
 * @date: 2021/6/8
 */
public class BaseUtil {


    /**
     * list转string类型
     * @param list
     * @return
     */
    public static String listToString(List<String> list){

        String str = list.toString();
        str= str.replace("[","").replace("]","").replace(" ","");
        return str;
    }

    /**
     * @description: Object to spring
     * @author: lig
     * @date: 2021/7/5
     */
    public static String objectToString(Object obj) {
        if (ObjectUtils.isEmpty(obj)) {
            return "";
        } else {
            return obj.toString();
        }

    }


    /**
     * 查看一个字符串是否可以转换为数字
     *
     * @param str 字符串
     * @return true 可以; false 不可以
     */
    public static boolean isStr2Num(String str) {
        try {
            Float.parseFloat(str);
//            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * @description: 获取不带扩展名的文件名
     * @author: lig
     * @date: 2021/6/8
     */
    public static String getFileName(String file) {
//        String filePath = "E:\\test\\test.dxf";
        String fileName = "";
        if (file.lastIndexOf("\\") > 0) {
            fileName = file.substring(file.lastIndexOf("\\") + 1);
        } else if (file.lastIndexOf("/") > 0) {
            fileName = file.substring(file.lastIndexOf("/") + 1);
        } else {
            fileName = file;
        }

        if (!fileName.equals("")) {
            String name = fileName.substring(0, fileName.lastIndexOf("."));
            return name;
        }
        return "";
    }


    /**
     * @description: list空元素
     * @author: lig
     * @date: 2021/6/4
     */
    public static boolean listIsNotNullElement(List list) {
        if (null != list && list.size() > 0 && null != list.get(0)) return true;
        return false;

    }


//    /**
//     * @description: 档案工具  构造分类号，获取最后的分类号
//     *
//     * @author: lig
//     * @date: 2021/8/11
//     */
//    public static void builClassNum(ArchivesDoc archivesDoc){
//        if(null != archivesDoc && !StringUtils.isEmpty(archivesDoc.getClassNum())){
//            archivesDoc.setClassNum(archivesDoc.getClassNum().substring(archivesDoc.getClassNum().lastIndexOf("/")+1));
//        }
//    }


//    public static void main(String[] args){
//        System.out.println("11:"+getFileName("http://192.168.1.112:7502/file/static/2-2020-0002-003(1).pdf"));
//    }



    /**
     * @description: 获取MAC 地址
     *
     * @author: lig
     * @date: 2021/9/15
     */
    public static String getMACAddress() {
        String address = "";
        String os = System.getProperty("os.name");
        if (os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
                String line;
                while ((line = br.readLine()) != null) {
//                    String newName=new String(line.getBytes("UTF-8"),"GBK");
                    if (line.indexOf("Physical Address") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        address = line.substring(index);
                        break;
                    }else  if (line.indexOf("物理地址") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        address = line.substring(index);
                        break;
                    }
                }
                br.close();
                return address.trim();
            } catch (IOException e) {
            }
        } else if (os.startsWith("Linux")) {
            String command = "/bin/sh -c ifconfig -a";
            Process p;
            try {
                p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.indexOf("HWaddr") > 0) {
                        int index = line.indexOf("HWaddr") + "HWaddr".length();
                        address = line.substring(index);
                        break;
                    }
                }
                br.close();
            } catch (IOException e) {
            }
        }
        address = address.trim();
        return address;
    }


    //public static void main(String[] args) throws IOException {
//        try {
//            Process process = Runtime.getRuntime().exec(new String[]{"wmic", "cpu", "get", "ProcessorId"});
//            process.getOutputStream().close();
//            Scanner sc = new Scanner(process.getInputStream());
//
////            // 判断是否还有输入
////            if (sc.hasNext()) {
////                String str1 = sc.next();
////                System.out.println("输入的数据为：" + str1);
////            }
//
//            String serial = sc.next();
//            System.out.println(serial);
//
//            String property = sc.next();
//            System.out.println(property);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        //System.out.println("|||||:"+getMACAddress());


    //}


}
